package cn.innosmart.aq.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.WorkLogListAdapter;
import cn.innosmart.aq.bean.WorkLog;
import cn.innosmart.aq.bean.WorkLogTemp;
import cn.innosmart.aq.util.INetworkInteractUtil;
import cn.innosmart.aq.util.NetworkInteractUtil;
import cn.innosmart.aq.view.activity.base.BaseActivity;

/**
 * Created by David on 2015/12/1.
 */
public class WorkLogListActivity extends BaseActivity implements INetworkInteractUtil {
    private final int EXIT = 0;
    private final int LOADINGDATA = 1;
    private Toolbar toolbar;
    private ListView lvWorkLog;
    private NetworkInteractUtil interactUtil;
    private Menu menu;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EXIT:
                    exit();
                    break;
                case LOADINGDATA:
                    break;
            }
        }
    };
    private WorkLogTemp workLogTemp;
    private WorkLogListAdapter workLogListAdapter;
    private ArrayList<WorkLog> worklogs = new ArrayList<>();
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            WorkLog worklog = worklogs.get(position);
            Intent intent = new Intent(WorkLogListActivity.this, WorkLogAddActivity.class);
            intent.putExtra("flag", "check");
            intent.putExtra("worklogtemp", workLogTemp.toJSonObject().toString());
            intent.putExtra("worklog", worklog.toJsonObject().toString());
            startActivityForResult(intent, 1);
        }
    };
    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.action_add:
                    Intent intent = new Intent(WorkLogListActivity.this, WorkLogAddActivity.class);
                    intent.putExtra("worklogtemp", workLogTemp.toJSonObject().toString());
                    intent.putExtra("flag", "add");
                    startActivityForResult(intent, 0);
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                    break;
            }
            return true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_log);
        initView();
        setBar();
        interactUtil = new NetworkInteractUtil(this);
        getData();
    }

    private void getData() {
        loadingDialog();
        interactUtil.getWorkLog(workLogTemp.getId());
    }

    private void initView() {
        lvWorkLog = (ListView) findViewById(R.id.ll_work_log);
        workLogListAdapter = new WorkLogListAdapter(this);
        lvWorkLog.setOnItemClickListener(onItemClickListener);
        lvWorkLog.setAdapter(workLogListAdapter);
        TextView tv = (TextView) findViewById(R.id.tv_empty);
        tv.setText(R.string.no_work_log_data);
        lvWorkLog.setEmptyView(tv);
        Intent intent = getIntent();
        String workLogStr = intent.getStringExtra("worklog");
        try {
            workLogTemp = new WorkLogTemp(new JSONObject(workLogStr));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void setBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        TextView tv = (TextView) toolbar.findViewById(R.id.tv_toolbar);
        tv.setText(workLogTemp.getTitle());
        ((ImageView) findViewById(R.id.iv_headicon)).setImageResource(R.drawable.toolbar_back);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
        findViewById(R.id.iv_headicon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.sendEmptyMessage(EXIT);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        toolbar.setNavigationIcon(null);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        getMenuInflater().inflate(R.menu.menu_work_log_add, menu);
        this.menu = menu;
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onNetworkInteractAllOver(int ret) {

    }

    @Override
    public void onNetworkInteractOver(int ret, String flag, Object object) {
        if (interactUtil.GETWORKLOG.equals(flag)) {
            hideloadingDialog();
            if (ret == 0) {
                JSONArray jsonResult = (JSONArray) object;
                JSONObject jsonParams;
                WorkLog worklog = null;
                worklogs.clear();
                for (int i = 0; i < jsonResult.length(); i++) {
                    try {
                        jsonParams = new JSONObject(jsonResult.getString(i));
                        worklog = new WorkLog(jsonParams);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    worklogs.add(worklog);
                }
                Collections.sort(worklogs, new Comparator<WorkLog>() {
                    @Override
                    public int compare(WorkLog lhs, WorkLog rhs) {
                        if (lhs.getTime() >= rhs.getTime())
                            return -1;
                        else
                            return 1;
                    }
                });
                workLogListAdapter.refresh(worklogs);
            } else {
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == 1) {
            getData();
        } else if (requestCode == 1 && resultCode == 1) {

        }
    }
}
